package com.whsxt.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.*;

/*
* 系统用户
* */
@ApiModel(value = "com-whsxt-domain-SysUser")
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "sys_user")
public class SysUser implements Serializable, UserDetails {
    @TableId(value = "user_id",type = IdType.AUTO)
    @ApiModelProperty(value = "")
    private Long userId;
    /*
    * 用户名
    * */
    @TableField(value = "username")
    @ApiModelProperty(value = "用户名")
    private String username;
    /*
    * 密码
    * */
    @TableField(value = "password")
    @ApiModelProperty(value = "密码")
    private String password;
    /*
    * 邮箱
    * */
    @TableField(value = "email")
    @ApiModelProperty(value = "邮箱")
    private String email;
    /*
    * 手机号
    * */
    @TableField(value = "mobile")
    @ApiModelProperty(value = "手机号")
    private String mobile;
    /*
    * 状态 0：禁用  1：正常
    * */
    @TableField(value = "status")
    @ApiModelProperty(value = "状态 0：禁用 1：正常")
    private Integer status;
    /*
    * 创建者ID
    * */
    @TableField(value = "create_user_id")
    @ApiModelProperty(value = "创建者ID")
    private Long createUserId;
    /*
    * 创建时间
    * */
    @TableField(value = "create_time")
    @ApiModelProperty(value = "创建时间")
    private Date createTime;
    /*
    * 用户所在的商城Id
    * */
    @TableField(value = "shop_id")
    @ApiModelProperty(value = "用户所在的商城Id")
    private Long shopId;
    /*
    * 权限集合。
    * 我们使用的是mybatis-plus，所以如果实体类中出现数据库的表格中
    * 没有出现的属性，我们要加上注解@TableField(exist=false)
    * */
    @TableField(exist=false)
    private List<String> auths;
    /*
    * 封装权限
    * */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //new 一个权限集合
        ArrayList<SimpleGrantedAuthority> simpleGrantedAuthorities=new ArrayList<>();
        //变成 oauth 认识的对象
        if(CollectionUtils.isEmpty(auths)){
            return Collections.emptyList();
        }
        //循环权限集合
        auths.forEach(auth->{
            if(!StringUtils.isEmpty(auth)){
                //分割
                String[] split=auth.split(",");
                for(String s:split){
                    //循环封装
                    simpleGrantedAuthorities.add(new SimpleGrantedAuthority(s));
                }
            }
        });
        return simpleGrantedAuthorities;
    }
    /*
    *我们要从 SecurityContextHolder 拿到当前用户的信息
    *因为一个系统里面需要用到用户的id的情况特别多，所以为了方便查询，我们将 username 改成 userid
    *因为 oauth 生成的 jwt 的格式是很严格的，不可以轻易的改变
    *  */
    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return String.valueOf(this.userId);
    }
    
    @Override
    public boolean isAccountNonExpired() {
        return this.status==1;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.status==1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.status==1;
    }

    @Override
    public boolean isEnabled() {
        return this.status==1;
    }

    public Long getUserId() {
        return this.userId;
    }

    public void setAuths(List<String> auths) {
    }
}
